package com.xiaoyu.algorithmimpl;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;

import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;

public class FileImpl {
	private static boolean found = false;

	private static String encoding = null;

	public static String SimpleEncoding(String fileName) throws Exception {
		BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
		int p = (bin.read() << 8) + bin.read();
		String code = null;
		switch (p) {
			case 61371:
				code = "UTF-8";
				break;
			case 65534:
				code = "Unicode";
				break;
			case 65279:
				code = "UTF-16BE";
				break;
			default:
				code = "GBK";
		}
		return code;
	}

	public static String cpdetector(URL url) {
		Charset charset = null;
		try {
			charset = CpDetector.codepageDetector.detectCodepage(url);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (charset != null) {
			return charset.name();
		}
		return null;
	}

	public static String Encoding(String stringValue) {
		Charset charset = null;
		try {
			InputStream inputStream = new ByteArrayInputStream(stringValue.getBytes());
			charset = CpDetector.codepageDetector.detectCodepage(inputStream, 3);
			if (charset != null)
				return charset.name();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String guestFileEncoding(File file) throws FileNotFoundException, IOException {
		return geestFileEncoding(file, new nsDetector());
	}

	public static String guestFileEncoding(File file, int languageHint) throws FileNotFoundException, IOException {
		return geestFileEncoding(file, new nsDetector(languageHint));
	}

	public static String guestFileEncoding(String path) throws FileNotFoundException, IOException {
		return guestFileEncoding(new File(path));
	}

	public static String guestFileEncoding(String path, int languageHint) throws FileNotFoundException, IOException {
		return guestFileEncoding(new File(path), languageHint);
	}

	private static String geestFileEncoding(File file, nsDetector det)
			throws FileNotFoundException, IOException {
		det.Init(new nsICharsetDetectionObserver() {
			public void Notify(String charset) {
				 FileImpl.found = true;
				 FileImpl.encoding = charset;
			}
		});
		BufferedInputStream imp = new BufferedInputStream(new FileInputStream(file));
		byte[] buf = new byte[1024];
		boolean done = false;
		boolean isAscii = true;
		int len;
		while ((len = imp.read(buf, 0, buf.length)) != -1) {
			if (isAscii) {
				isAscii = det.isAscii(buf, len);
			}
			if ((!(isAscii)) && (!(done)))
				;
			done = det.DoIt(buf, len, false);
		}
		det.DataEnd();
		if (isAscii) {
			encoding = "ASCII";
			found = true;
		}
		if (!(found)) {
			String[] prob = det.getProbableCharsets();
			if (prob.length > 0) {
				encoding = prob[0];
			} else
				return null;
		}
		return encoding;
	}
}